package com.njtoyo.taxi.admin.logic.business.ride;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.njtoyo.taxi.admin.library.common.Enum;
import com.njtoyo.taxi.admin.library.payment.cmb.CmbPayClient;
import com.njtoyo.taxi.admin.library.payment.common.CmbRefundInfo;
import com.njtoyo.taxi.admin.mapper.master.entity.*;
import com.njtoyo.taxi.admin.mapper.master.enums.RideOrderPaidThrough;
import com.njtoyo.taxi.admin.mapper.master.enums.RideOrderState;
import com.njtoyo.taxi.admin.mapper.master.mapper.SimpleCallCustomerMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.SimpleCallInfoServiceFeePrepayOrderMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.SimpleCallRideMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.SimpleCallRideOrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
@Slf4j
public class SimpleCallOrderLogic {
    @Autowired
    private SimpleCallRideOrderMapper simpleCallRideOrderMapper;
    @Autowired
    private SimpleCallInfoServiceFeePrepayOrderMapper simpleCallInfoServiceFeePrepayOrderMapper;
    @Autowired
    private SimpleCallRideMapper simpleCallRideMapper;
    @Autowired
    private SimpleCallCustomerMapper simpleCallCustomerMapper;

    public SimpleCallRideOrder getPaidOrderByRideId(Long rideId) {
        SimpleCallRideOrder simpleCallRideOrder = simpleCallRideOrderMapper.selectOne(Wrappers.<SimpleCallRideOrder>lambdaQuery()
                .eq(SimpleCallRideOrder::getRideId, rideId));
        if (Objects.nonNull(simpleCallRideOrder) && simpleCallRideOrder.getIsOnline() && simpleCallRideOrder.getIsPaid()) {
            return simpleCallRideOrder;
        }
        return null;
    }


    public boolean cmbRefund(SimpleCallRideOrder order, Enum.RideRefundWxAppType wxAppType) {
        log.info(String.format("发起招商退款，RideOrder：[%s]；类型：[%s]", order, wxAppType));
        //退信息服务费
        SimpleCallInfoServiceFeePrepayOrder prepayOrder = simpleCallInfoServiceFeePrepayOrderMapper.selectOne(Wrappers.<SimpleCallInfoServiceFeePrepayOrder>lambdaQuery()
                .eq(SimpleCallInfoServiceFeePrepayOrder::getSimpleCallRideId, order.getRideId()));
        if (ObjectUtil.isNotNull(prepayOrder)) {
            prepayOrder.setIsRefund(true);
            String refundId = IdUtil.getSnowflake(1, 1).nextIdStr();
            String txnAmtStr = Convert.toStr(prepayOrder.getAmount().multiply(new BigDecimal(100)).intValue());
            Map<String, String> refundMap = CmbPayClient.refund(refundId, prepayOrder.getNumber(), "订单取消退款", prepayOrder.getNumber(), refundId, txnAmtStr, txnAmtStr);
            String errCodeAddAmount = MapUtil.getStr(refundMap, "errCode");
            if (StrUtil.isNotBlank(errCodeAddAmount)) {
                String respMsg = MapUtil.getStr(refundMap, "respMsg");
                log.info("一键叫车信息预付费退款--失败,失败原因：{}", order, respMsg);
                return false;
            }
            String respPreCode = MapUtil.getStr(refundMap, "respCode");
            if ("SUCCESS".equals(respPreCode)) {
                SimpleCallRideOrder simpleCallRideOrder = simpleCallRideOrderMapper.selectOne(Wrappers.<SimpleCallRideOrder>lambdaQuery().eq(SimpleCallRideOrder::getRideId, order.getRideId()));
                //退行程费用
                String refundIdOrder = IdUtil.getSnowflake(1, 1).nextIdStr();
                String txnAmtOrder = Convert.toStr(simpleCallRideOrder.getAmount().multiply(new BigDecimal(100)).intValue());
                Map<String, String> refundOrderMap = CmbPayClient.refund(refundIdOrder, simpleCallRideOrder.getNumber(), "订单取消退款", simpleCallRideOrder.getNumber(), refundIdOrder, txnAmtOrder, txnAmtOrder);
                String respOrderCode = MapUtil.getStr(refundOrderMap, "respCode");
                if ("SUCCESS".equals(respOrderCode)) {
                    SimpleCallRideOrder update = new SimpleCallRideOrder();
                    update.setId(simpleCallRideOrder.getId());
                    update.setIsRefund(true);
                    simpleCallRideOrderMapper.updateById(update);
                    return true;
                }
                String respMsg = MapUtil.getStr(refundOrderMap, "respMsg");
                log.info("一键叫车车费退款--失败,失败原因：{}", order, respMsg);
            }
        } else {//无信息服务费
            SimpleCallRideOrder simpleCallRideOrder = simpleCallRideOrderMapper.selectOne(Wrappers.<SimpleCallRideOrder>lambdaQuery().eq(SimpleCallRideOrder::getRideId, order.getRideId()));
            //退行程费用
            String refundIdOrder = IdUtil.getSnowflake(1, 1).nextIdStr();
            String txnAmtOrder = Convert.toStr(simpleCallRideOrder.getCustomerFarePaid().multiply(new BigDecimal(100)).intValue());
            Map<String, String> refundOrderMap = CmbPayClient.refund(refundIdOrder, simpleCallRideOrder.getNumber(), "订单取消退款", simpleCallRideOrder.getNumber(), refundIdOrder, txnAmtOrder, txnAmtOrder);
            String respOrderCode = MapUtil.getStr(refundOrderMap, "respCode");
            if ("SUCCESS".equals(respOrderCode)) {
                SimpleCallRideOrder update = new SimpleCallRideOrder();
                update.setId(simpleCallRideOrder.getId());
                update.setIsRefund(true);
                simpleCallRideOrderMapper.updateById(update);
                return true;
            }
            String respMsg = MapUtil.getStr(refundOrderMap, "respMsg");
            log.info("一键叫车车费退款--失败,失败原因：{}", order, respMsg);
        }
        return false;
    }
}
